import game from "./game";
import ufoCtrl from "./ufoCtrl";


const { ccclass, property } = cc._decorator;

@ccclass
export default class hero extends cc.Component {

    // gameSc = new game();

    ufoTime = 4;

    isDie: boolean;

    onLoad() {
        this.init();
    }

    start() {

    }

    init() {
        this.isDie = false;
        this.normal()
    }

    normal() {
        this.node.active = true;
        let anim = this.getComponent(cc.Animation)
        anim.play("heroNormal");

    }

    hit() {

    }

    die() {
        this.isDie = true;
        let anim = this.getComponent(cc.Animation)
        // if(this.isDie==true){
        //     this.node.off('touchmove');
        // }
        anim.play("heroBoom");
        let gg = cc.find("Canvas");
        let sc = gg.getComponent(game);

    }

    heroBoomOver() {
        this.node.active = false;
    }

    // callback = function () {
    //     let gg = cc.find("Canvas");
    //     let sc = gg.getComponent(game);
    //     if (sc.gameType=1) {
    //         var interval = 0;
    //         // 重复次数
    //         var repeat = 180;
    //         // 开始延时
    //         var delay = 0;
    //         this.schedule(function () {
    //             // 这里的 this 指向 component
    //             sc.creatB()
    //             sc.creatB2()

    //         }, interval, repeat, delay);
            
    //     }else{
    //         this.unschedule(this.callback);
    //     }
        
    // }

    
    callback = function () {
        let gg = cc.find("Canvas");
        let sc = gg.getComponent(game);
        sc.creatB2()
        sc.creatB3()
        if (sc.gameType!=1) {
            
            this.unschedule(this.callback);
        }
    }
    

    stopUfo(){
        this.schedule(this.callback, 0.2,25);
    }

    onCollisionEnter(other, self) {
        if (self.tag == 10 && this.isDie == false && (other.tag == 111 || other.tag == 2 || other.tag == 3)) {
            this.isDie = true;
            cc.find('Canvas').getComponent(game).gameType = 3;
            this.die();
            let gg = cc.find("Canvas");
            let sc = gg.getComponent(game);
            sc.playSoundHeroDie();
            cc.find('Canvas').getComponent(game).gameOverOver();
        }
        else if (other.tag == 8) {
            let gg = cc.find("Canvas");
            let sc = gg.getComponent(game);
            let js = other.node.getComponent(ufoCtrl)
            if (js && js.isGet == false) {
                js.hit();
                console.log("shuangqiang");
               this.stopUfo();

                
                



                //console.log("ufo++");

            }
        }
    }
    update(dt) {

    }
}
